﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="GetFiles.cs" company="Collaboris Ltd.">
//   Copyright (c) Collaboris Ltd. All rights Reserved.
// </copyright>
// <summary>
//   Uploads a File (or Directory of files) to an FTP Location
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Collaboris.Saf.Actions.Ftp
{
    using System;
    using System.Collections.Generic;

    using Engine;

    using SharePoint.Wss.Attributes;

    using Utils;

    /// <summary>
    /// The get files.
    /// </summary>
    [CollaborisClass(
          Title = "FTP Upload",
          ReleaseStatus = ReleaseStatus.Stable,
          DisableDocumentation = false,
          ContactAuthorEmail = "Support@Collaboris.co.uk",
          Summary = "This action will FTP one or many files to a destination FTP server.",
          UndoImplemented = false,
          ClassGuid = "{156E17A5-A7BA-4e1f-8A48-1BC341A7C614}"
      )
    ]
    public class FtpUpload : ActionBase<Entities.FtpUploadInfo>
    {
        /// <summary>
        /// Executes the Action using the values passed in by the Data Entity
        /// and the <see cref="SafContext"/>.
        /// </summary>
        /// <param name="context">
        /// A valid <see cref="SafContext"/> containing properties to run the Action with.
        /// </param>
        /// <param name="entity">
        /// A Valid <see cref="T"/> containing the parameters with which to run the Action with.
        /// </param>
        /// <returns>
        /// A <see cref="ProcessStatus"/> signifying the outcome of processing the Action.
        /// </returns>
        protected override ProcessStatus DoAction(SafContext context, Entities.FtpUploadInfo entity)
        {
            ProcessStatus status = ProcessStatus.Success;
            foreach (string fileName in entity.FileNames)
            {
                try
                {
                    FtpHelper.UploadFile(entity.FtpAddress, fileName, entity.UserName, entity.Password);
                }
                catch(Exception exception)
                {
                    status = ProcessStatus.Warning;
                    TraceHelper.TraceError(TraceSwitch, "FtpUpload", exception.Message);
                    TraceHelper.TraceWarning(TraceSwitch, "FtpUpload", "Unable to Ftp this file '{0}'", fileName);
                    if (this.StopOnDoException)
                        throw;
                }
            }

            return status;
        }


        /// <summary>
        /// Checks the Data entity used by the Action for Validity
        /// </summary>
        /// <param name="context">
        /// The <see cref="SafContext"/> passed in by the SAF Engine.
        /// </param>
        /// <param name="entity">
        /// The data entity to Validate.
        /// </param>
        /// <param name="errors">
        /// List of <see cref="ValidationError"/>s
        /// </param>
        /// <param name="mode">
        /// <see cref="ProcessMode"/>signalling wheter we are Doing, or Undoing the action.
        /// </param>
        /// <returns>
        /// false if invalid; otherwise true.
        /// </returns>
        protected override bool IsValid(
                SafContext context, Entities.FtpUploadInfo entity, ref List<ValidationError> errors, ProcessMode mode)
        {
            this.Trace.TraceMethodStart("IsValid");

            int currentCount = errors.Count;

            // Validate each object now.
            ValidateStringForNull(entity.FtpAddress, "FtpUploadInfo.FtpAddress", ref errors);
            ValidateObjectForNull(entity.FileNames, "FtpUploadInfo.FileNames", ref errors);

            this.Trace.TraceMethodEnd("IsValid");

            // Have we added errors ?
            return errors.Count == currentCount;
        }
    }
}

